package collections.list;

import java.util.*;

/**
 * Collection:
 * 作业
 * 1.区分： Cloning an Array, Copying Array Range
 * 2.写一个方法，用两种循环方式将字符串全部打印出来  {"ddd","ddd","fff","shr","rwger"}
 */
public class CcollectionUtils {
    public static void main(String[] args) {

        Collection<String> collection = new ArrayList<>();

        List<Integer> numList = new ArrayList<>(Arrays.asList(3, 2, 3, 2, 1, 1, 2, 5, 6, 3, 3, 4, 2, 7));
        CcollectionUtils utils = new CcollectionUtils();
        System.out.println("原集合：" + numList);
        List<Integer> instinctNumber1 = utils.instinctNumber1(numList);
        List<Integer> instinctNumber2 = utils.instinctNumber2(numList);
        System.out.println("去重后集合1：" + instinctNumber1);
        System.out.println("去重后集合2：" + instinctNumber2);
        utils.number3(numList);
        //排序
        //冒泡排序
        ArrayList<Integer> numList1 = new ArrayList<>(Arrays.asList(3, 2, 3, 2, 1, 1, 2, 5, 6, 3, 3, 4, 2, 7));
        List<Integer> orderNumber3 = utils.bubbleSortNumber(numList1);
        System.out.println();
        System.out.println("冒泡排序：" + orderNumber3);
        System.out.println();
        List<Integer> orderNumber4 = utils.quickOrderNumber(numList1);
        System.out.println("快速排序：" + orderNumber4);

        // 找出一组数据中的最大值最小值的两个数字
        System.out.println("最大值：" + orderNumber3.get(orderNumber3.size() - 1));
        System.out.println("最小值：" + orderNumber3.get(0));

        ArrayList<Integer> numList2 = new ArrayList<>(Arrays.asList(3, 2, 3, 2, 1, 1, 2, 5, 6, 3, 3, 4, 2, 7));
        numList2.sort(Integer::compareTo);
        System.out.println(numList2);

    }

    /**
     * @param numList 原数组
     * @return 排序后的数组
     * 使用的排序思路是：冒泡
     */
    public List<Integer> bubbleSortNumber(List<Integer> numList) {
        for (int i = 0; i < numList.size() - 1; i++) {
            for (int j = numList.size() - 1; j > i; j--) {
                int num1 = numList.get(j);
                Integer num2 = numList.get(j - 1);
                //替换元素
                if (num1 < num2) {
                    int temp = num1;
                    numList.set(j, num2);
                    numList.set(j - 1, temp);
                }
            }
        }
        return numList;
    }

    /**
     * @param numList 原数组
     * @return 排序后的数组
     * 使用的排序思路是：快速排序  index 索引
     */
    public List<Integer> quickOrderNumber(List<Integer> numList) {
        for (int i = 0; i < numList.size(); i++) {
            int mixNumberIndex = i;
            for (int j = i + 1; j < numList.size(); j++) {
                int mixNumber = numList.get(mixNumberIndex);
                int secondNumber = numList.get(j);
                if (secondNumber < mixNumber) {
                    mixNumberIndex = j;
                }
            }
            if (mixNumberIndex != i) {
                int temp = numList.get(i);//临时变量
                int mixNumber = numList.get(mixNumberIndex);
                numList.set(i, mixNumber);
                numList.set(mixNumberIndex, temp);
            }
        }
        return numList;
    }

    /**
     * 建立一个新的集合，然后将元素放进去，在放入的时候，和新的元素进行比对，如果新的里面有，就不放
     *
     * @param originNumber
     * @return
     */
    public List<Integer> instinctNumber1(List<Integer> originNumber) {
        List<Integer> collection = new ArrayList<>();
        for (int i = 0; i < originNumber.size(); i++) {
            Integer firstNumber = originNumber.get(i);
            if (!collection.contains(firstNumber)) {
                collection.add(firstNumber);
            }
        }
        return collection;
    }

    /**
     * 两种循环方法
     *
     * @param originNumber
     */
    public void number3(List<Integer> originNumber) {
        //第一种：for循环
        System.out.println("第一种循环：");
        for (int i = 0; i < originNumber.size(); i++) {
            System.out.print(originNumber.get(i) + " ,");
        }
        //第二种：迭代器循环
        System.out.println();
        System.out.println("第二种循环：");
        Iterator<Integer> iterator = originNumber.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ,");
        }
        //java 8的 Stream 流  Lamdba 表达式
        System.out.println();
        System.out.println("第三种循环：");
        originNumber.stream().forEach(e -> System.out.print(e + " ,"));

    }

    public List<Integer> instinctNumber2(List<Integer> originNumber) {
        int size = originNumber.size();
        //第一次循环
        for (int i = 0; i < size; i++) {
            //去出来第一个元素
            if (i < originNumber.size()) {
                Integer firstNumber = originNumber.get(i);
                Integer flage = 0;
                //第二次循环
                Iterator<Integer> iterator = originNumber.iterator();
                while (iterator.hasNext()) {
                    Integer next = iterator.next();
                    //首次进来执行判断当前元素
                    if (flage == 0 && firstNumber == next) {
                        flage = 1;
                        continue;
                    }
                    if (flage == 1 && firstNumber == next) {
                        iterator.remove();
                    }
                }

            }
        }
        return originNumber;
    }

    /**
     * 希望通过二分查找算法找到目标数据
     *
     * @param listNums 已经排好序的集合
     * @param target   希望找到的目标数字，然后获取到这个数据的索引
     * @return 当没有找到的时候，返回-1；
     */
    public int binarySearch(List<Integer> listNums, int target) {
        //使用二分法查找元素
        int low = 0;
        int high = listNums.size();
        int middle = (low + high) / 2;
        while (low <= high) {
            int middleNums = listNums.get(middle);
            if (target == middleNums) {
                System.out.println("下标索引：" + middle + "，数字为：" + target);
                break;
            }
            if (target <middleNums ) {
                low = low + 1;
                high = middle - 1;
                middle = (low + high) / 2;
            } else if (target > middleNums) {
                low = middle + 1;
                high = high - 1;
                middle = (low + high) / 2;
            }

        }


        return -1;
    }

}
